#include "weather.h"

extern int h_errno;

void get_localweather(void)
{
    struct hostent *ht = gethostbyname("api.k780.com");
    if(!ht){
        fprintf(stderr,"gethostbyname:%s\n",strerror(h_errno));
        exit(-1);
    }

    //1.创建socket描述符
    int sockfd = socket(AF_INET,SOCK_STREAM,0);
    if(sockfd==-1){
        perror("socket");
        exit(-1);
    }

    //2.准备服务器通信地址
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;//协议簇
    addr.sin_port = htons(80);//网络字节顺序端口号
    addr.sin_addr = *(struct in_addr *)ht->h_addr;//网络字节顺序IP

    //3.连接服务器
    int res = connect(sockfd,(struct sockaddr *)&addr,sizeof(addr));
    if(res==-1){
        perror("connect");
        exit(-1);
    }
    printf("connect OK!\n");

    //4.构造http请求报文
    
    char protolHead[8192] = {"GET /?app=ip.local&appkey=77109&sign=fa4005654a4ac1ea2f23b72df28171dd&format=json HTTP/1.1\r\n"
                            "Host:api.k780.com:80\r\n"
                            "Connection:keep-alive\r\n"
                            "\r\n"};

    //5.发送http请求
    res = send(sockfd,protolHead,strlen(protolHead),0);
    if(res<=0){
        perror("send");
        exit(-1);
    }

    //6.接收服务器的响应
    char recvBuf[4096] = {};
    res = recv(sockfd,recvBuf,sizeof(recvBuf),0);
    if(res<=0){
        perror("recv");
        exit(-1);
    }

    //printf("%s\n",recvBuf);
    char *start = strchr(recvBuf,'{');
    //7.解析json
    cJSON* cjson_test = NULL;
    cJSON* cjson_result = NULL;
    cJSON* cjson_ip = NULL;
    cjson_test = cJSON_Parse(start);
    if(cjson_test == NULL){
        printf("cjson_test == NULL\n");
        exit(-1);
    }
    cjson_result = cJSON_GetObjectItem(cjson_test,"result");
    if(cjson_result == NULL){
        printf("cjson_result == NULL\n");
        exit(-1);
    }
    //8.获取ip
    cjson_ip = cJSON_GetArrayItem(cjson_result,0);
    //printf("当地ip:%s\n",cjson_ip->valuestring);

    sprintf(protolHead,"GET /?app=weather.today&cityIp=%s&appkey=77109&sign=fa4005654a4ac1ea2f23b72df28171dd&format=json HTTP/1.1\r\n"
                            "Host:api.k780.com:80\r\n"
                            "Connection:keep-alive\r\n"
                            "\r\n",cjson_ip->valuestring);
                            
    //5.发送http请求
    res = send(sockfd,protolHead,strlen(protolHead),0);
    if(res<=0){
        perror("send");
        exit(-1);
    }

    res = recv(sockfd,recvBuf,sizeof(recvBuf),0);
    if(res<=0){
        perror("recv");
        exit(-1);
    }

    //printf("%s\n",recvBuf);

    start = strchr(recvBuf,'{');
    
    //7.解析json
    cJSON* cjson_humidity = NULL;
    //cJSON* cjson_temperature = NULL;
    cJSON* cjson_temperature_curr = NULL;
    cJSON* cjson_aqi = NULL;
    cjson_test = cJSON_Parse(start);
    if(cjson_test == NULL){
        printf("cjson_test == NULL\n");
        exit(-1);
    }
    cjson_result = cJSON_GetObjectItem(cjson_test,"result");
    if(cjson_result == NULL){
        printf("cjson_result == NULL\n");
        exit(-1);
    }
    cjson_humidity = cJSON_GetArrayItem(cjson_result,8);
    humidity_curr = atoi(cjson_humidity->valuestring);
    //printf("地方：%s\n",cjson_citynm->valuestring);
    //cjson_temperature = cJSON_GetArrayItem(cjson_result,6);
    //printf("温度最大值和最小值：%s\n",cjson_temperature->valuestring);
    cjson_temperature_curr = cJSON_GetArrayItem(cjson_result,7);
    temperature_curr = atoi(cjson_temperature_curr->valuestring);
    //printf("当前温度：%s\n",cjson_temperature_curr->valuestring);
    cjson_aqi = cJSON_GetArrayItem(cjson_result,9);
    AQI_curr = atoi(cjson_aqi->valuestring);
    //printf("天气状况：%s\n",cjson_weather->valuestring);
 
   close(sockfd);
    return;
}

//http://api.k780.com/?app=ip.get&ip=%s&appkey=77109&sign=fa4005654a4ac1ea2f23b72df28171dd&format=json"
